<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="resetFormData">
        创建流程
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%"
      row-key="id"
      border
      @expand-change="expandChange"
    >
      <el-table-column type="expand">
        <template  slot-scope="{row}">
          <el-tabs active-name="first" type="border-card">
            <el-tab-pane label="节点管理" name="first">
              <FlowNodeList :flow-id="row.id" />
            </el-tab-pane>
            <el-tab-pane label="显示控制" name="second">
              <FlowParamsList :flow-id="row.id" />
            </el-tab-pane>
            <el-tab-pane label="绑定菜单" name="last">
              <FlowBindList :flow-id="row.id" />
            </el-tab-pane>
          </el-tabs>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="流程名称"
      />
      <el-table-column
        prop="actionBeanName"
        label="节点监控(BeanName)"
      />
      <el-table-column
        prop="createTime"
        label="创建时间"
      />
      <el-table-column label="操作" width="290" align="center">
        <template slot-scope="{row}">
          <el-button type="primary" @click="updateFormData(row)">编辑流程 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" style="width: 1100px;margin: auto">
      <el-form ref="dataForm" :rules="rules" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="流程名称" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>
        <el-form-item label="节点监控" prop="actionBeanName">
          <el-input v-model="fromTemp.actionBeanName" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="createFlow">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="nodeFormVisible" style="width: 1100px;margin: auto">
      <el-form ref="nodeDataForm" :rules="rules" :model="fromTemp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="节点名称" prop="name">
          <el-input v-model="fromTemp.name" />
        </el-form-item>
        <el-form-item label="节点排序" prop="sort">
          <el-input v-model="fromTemp.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="margin-right: 50px">
        <el-button @click="nodeFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveNode">
          确认
        </el-button>
      </div>
    </el-dialog>
    <el-dialog title="配置节点" :visible.sync="dialogButtonFormVisible" style="margin: auto">
      <SystemRoleList :key="updateList" :userid="userId" />
    </el-dialog>
  </div>
</template>
<script>
import { GetFlowList, GetFlowNodeList, SaveFlow, SaveFlowNode } from '@/api/SystemManagerApi'
import SystemRoleList from '@/views/SystemSetting/SystemUser/components/SystemRoleList'
import FlowNodeList from '@/views/SystemSetting/SystemFlow/components/FlowNodeList'
import FlowBindList from '@/views/SystemSetting/SystemFlow/components/FlowBindList'
import FlowParamsList from '@/views/SystemSetting/SystemFlow/components/FlowParamsList'

export default {
  components: { FlowBindList, FlowNodeList, SystemRoleList, FlowParamsList },
  data() {
    return {
      rules: {
        sort: [{ required: true, message: 'type is required', trigger: 'change' }],
        name: [{ required: true, message: 'type is required', trigger: 'change' }]
      },
      title: '编辑流程',
      fromTemp: {
        id: null,
        name: null,
        actionBeanName: null
      },
      carName: '节点管理',
      dialogFormVisible: false,
      dialogButtonFormVisible: false,
      nodeFormVisible: false,
      listLoading: false,
      list: [],
      userId: null,
      updateList: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      GetFlowList().then(response => {
        const tempData = response.data
        tempData.forEach((obj) => {
          obj['hasChildren'] = true
        })
        this.list = tempData
        this.listLoading = false
      })
    },
    resetFormData() {
      this.fromTemp = {
        id: null,
        name: ''
      }
      this.dialogFormVisible = true
    },
    updateFormData(row) {
      this.fromTemp = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    createFlow() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          SaveFlow(this.fromTemp).then((response) => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        }
      })
    },
    showRoleList(row) {
      this.userId = row.id
      this.updateList = this.uuid()
      this.$nextTick(() => {
        this.dialogButtonFormVisible = true
      })
    },
    uuid() {
      function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
      }
      return (S4() + S4() + '-' + S4() + '-' + S4() + '-' + S4() + '-' + S4() + S4() + S4())
    },
    load(tree, treeNode, resolve) {
      GetFlowNodeList(tree.id).then(response => {
        const data = response.data
        resolve(data)
      })
    },
    resetNodeForm(row) {
      this.fromTemp = {
        id: null,
        name: '',
        flowId: row.id
      }
      this.$nextTick(() => {
        this.$refs['nodeDataForm'].clearValidate()
      })
      this.nodeFormVisible = true
    },
    saveNode(row) {
      this.$refs['nodeDataForm'].validate((valid) => {
        if (valid) {
          SaveFlowNode(this.fromTemp).then((response) => {
            this.nodeFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    expandChange(row, expanded) {
    }
  }
}
</script>
